package com.mall.common.domain.manage;

import static com.mall.common.Constants.ROLE_SEPARATOR;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PreRemove;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sf.json.JSONObject;

import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mall.common.domain.PriorityEntity;
import com.mall.common.util.PropertiesHelper;

/**
 * 实体类 - 操作表
 * @author zhenglong
 */
@Entity
@Table(name="s_rbac_operate")
public class Operate extends PriorityEntity {
	
	private static final long serialVersionUID = -3817521535097551737L;
	
	private String name;//操作名称
	private String description;//描述
	private Integer type;//是否授权动作(1:授权动作， 2:菜单所属动作)
	private Menu menu;//按钮所属菜单
	private String btnIcon;//按钮图标
	private String url;//url
	private String pattern;//匹配模式
	private String target;
	private String html;
	
	@JsonIgnore private Boolean isDiaplayInToolBar;//按钮图标是否显示在工具栏上
	@JsonIgnore private Set<Role> roleSet;// 权限
	
	@PreRemove
	public void preRemove(){
		for(Role role : roleSet){
			role.getOperateSet().remove(this);
		}
	}
	
	// 获取权限字符串（以分隔符间隔）
	@Transient
	@JsonIgnore
	public String getRoleSetString() {
		//增加系统管理员的权限
		String system_manager_role = PropertiesHelper.getPropertiesValue("config", "system_manager_role");
		if(this.roleSet == null || this.roleSet.size() == 0) {
			return system_manager_role;
		}
		StringBuffer stringBuffer = new StringBuffer();
		for (Role role : this.roleSet) {
			stringBuffer.append(ROLE_SEPARATOR + role.getValue());
		}
		stringBuffer.append(ROLE_SEPARATOR + system_manager_role);
		
		if (stringBuffer.length() > 0) {
			stringBuffer.deleteCharAt(0);
		}
		return stringBuffer.toString();
	}
	
	@Transient
	@JsonIgnore
	public JSONObject getJson(){
		JSONObject obj = new JSONObject();
		obj.put("id", this.getId());
		obj.put("name", this.getName());
		obj.put("url", this.getUrl());
		obj.put("html", this.getHtml());
		obj.put("btnIcon", this.getBtnIcon());
		obj.put("type", "operate");
		obj.put("pattern", this.getPattern());
		obj.put("parentId", this.getMenu().getId());
		return obj;
	}
	
	@Column(length=50)
	@NotBlank
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length=255)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(length=1)
	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Column(length=200)
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Column(length=2000)
	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	@Column(length=50)
	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	@Column(length=500)
	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	@Column(length=200,name="btnIcon")
	public String getBtnIcon() {
		return btnIcon;
	}

	public void setBtnIcon(String btnIcon) {
		this.btnIcon = btnIcon;
	}

	@Column(length=1,name="isDiaplayInToolBar",columnDefinition="tinyint(1) default 0")
	public Boolean getIsDiaplayInToolBar() {
		return isDiaplayInToolBar;
	}

	public void setIsDiaplayInToolBar(Boolean isDiaplayInToolBar) {
		this.isDiaplayInToolBar = isDiaplayInToolBar;
	}

	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="menu_id")
	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	@ManyToMany(mappedBy = "operateSet", fetch = FetchType.EAGER)
	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<Role> roleSet) {
		this.roleSet = roleSet;
	}

}